from typing import List
from fastapi import APIRouter, Depends, Security
from sqlalchemy.ext.asyncio import AsyncSession

from apis.deps import get_db, get_current_user
from schemas import CronTabCreate, CronTabUpdate, CronTabOut as CronTab, Result, ResultPlus
from crud import cronTab
from utils import resp_200, IdNotExist

router = APIRouter()


@router.post('/createCronTab', summary='新增定时任务')
async def createCronTab(obj_in: CronTabCreate, db: AsyncSession = Depends(get_db)):
    await cronTab.create(db, obj_in)
    return resp_200(data=obj_in, msg='新增定时任务成功')


@router.get('/getCronTab', summary='获取定时任务列表')
async def getCronTab(pageIndex: int = 1, pageSize: int = 10, queryName: str = '', db: AsyncSession = Depends(get_db)):
    cronTab_list = await cronTab.get_multi(db=db, pageIndex=pageIndex, pageSize=pageSize, queryName=queryName)
    return resp_200(data=cronTab_list, msg='获取定时任务列表成功')


@router.put('updateCronTab', summary='更新定时任务')
async def updateCronTab(id: int, obj_in: CronTabUpdate, db: AsyncSession = Depends(get_db)):
    await cronTab.update(db, id, obj_in)
    return resp_200(data=obj_in, msg='更新定时任务成功')


@router.delete('/deleteCronTab', summary='删除定时任务')
async def deleteCronTab(id: int, db: AsyncSession = Depends(get_db)):
    await cronTab.remove(db, id)
    return resp_200(data=id, msg='删除定时任务成功')
